<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="库房编号" prop="roomId">
						<el-select v-model="form.roomId" clearable filterable remote :remote-method="remoteMethod" style="width: 100%" placeholder="输入库房编号或地址查询">
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="进入类型" prop="accessType">
						<sc-select v-model="form.accessType" :params="{code:'accessType'}" :apiObj="$API.archives.archivesDic.getArchivesDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="人员类型" prop="isOut">
						<el-radio-group v-model="form.isOut">
							<el-radio-button label="0">内部</el-radio-button>
							<el-radio-button label="1">外部</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="联系电话" prop="phone">
						<el-input v-model="form.phone" placeholder="联系电话" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24" v-if="form.isOut=='0'">
				<el-col :lg="12">
					<el-form-item label="部门名称" prop="deptId">
						<select-dept-dialog v-model:deptId="form.deptId" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="人员姓名" prop="accountId">
						<select-user-dialog v-model:accountId="form.accountId" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24" v-if="form.isOut=='1'">
				<el-col :lg="24">
					<el-form-item label="人员单位" prop="orgName">
						<el-input v-model="form.orgName" placeholder="人员单位" maxlength="18" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="进入人员" prop="userName">
						<el-input v-model="form.userName" placeholder="进入人员" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="证件号码" prop="cardId">
						<el-input v-model="form.cardId" placeholder="证件号码" maxlength="18" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="进入时间" prop="accessTime">
						<el-date-picker v-model="form.accessTime" placeholder="进入时间" type="datetime" value-format="YYYY-MM-DD HH:mm" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="离开时间" prop="leaveTime">
						<el-date-picker v-model="form.leaveTime" placeholder="离开时间" type="datetime" value-format="YYYY-MM-DD HH:mm" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="进入事由" prop="reason">
				<el-input type="textarea" v-model="form.reason" placeholder="进入事由" maxlength="380" clearable></el-input>
			</el-form-item>
			<el-form-item label="备注说明" prop="remark">
				<el-input type="textarea" v-model="form.remark" placeholder="备注说明" maxlength="380" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 认</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog.vue";
import selectDeptDialog from "@/views/module/selectDeptDialog.vue";
export default {
	emits: ['success', 'closed'],
	components: {
		selectDeptDialog,
		selectUserDialog,
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			titleMap: {
				add: '出入登记',
				edit: '出入登记',
			},
			fileList:[],
			form: {
				recordId:'',
				roomId: '',
				accessType: '',
				isOut: '0',
				phone: '',
				deptId:'',
				accountId:'',
				orgName:'',
				userName: '',
				cardId:'',
				accessTime:'',
				leaveTime:'',
				remark: '',
				reason:'',
			},
			rules: {
				roomId: [
					{required: true, message: '库房编号不能为空', trigger: 'blur'}
				],
				accessType:[
					{required: true, message: '出入类型不能为空', trigger: 'blur'}
				],
				phone:[
					{required: true, message: '联系电话不能为空', trigger: 'blur'}
				],
				reason:[
					{required: true, message: '出入事由不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.remoteMethod("")
	},
	methods: {
		async remoteMethod(query){
			let res = await this.$API.archives.archivesRoom.getArchivesRoomListForSelect.get({keyword:query});
			if (res.code == 200) {
				this.options = res.data
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.archives.archivesAccess.insertArchivesAccess.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.archives.archivesAccess.updateArchivesAccess.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.roomId = data.roomId
			this.form.accessType = data.accessType
			this.form.isOut = data.isOut
			this.form.accountId =data.accountId
			this.form.deptId = data.deptId
			this.form.phone = data.phone
			this.form.orgName = data.orgName
			this.form.userName = data.userName
			this.form.cardId = data.cardId
			this.form.accessTime = data.accessTime
			this.form.leaveTime = data.leaveTime
			this.form.reason = data.reason
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
